package org.ioc.com.book6.service;

import org.ioc.com.book6.enums.BookStatus;
import org.ioc.com.book6.mapper.BookInfoMapper;
import org.ioc.com.book6.model.BookInfo;
import org.ioc.com.book6.model.PageRequest;
import org.ioc.com.book6.model.PageResult;
import org.ioc.com.book6.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookInfoService {
    @Autowired
    private BookInfoMapper bookInfoMapper;
    //插入图书
    public Integer insert(BookInfo bookInfo){
        Integer insert = bookInfoMapper.queryInsertBook(bookInfo);
        return insert;
    }
    //查询图书--分页查询
    public PageResult<BookInfo> select(PageRequest pageRequest) {
        List<BookInfo> bookInfos = bookInfoMapper.querySelect(pageRequest.getOffset(), pageRequest.getPageSize());
        Integer count = bookInfoMapper.count();
        for (BookInfo bookInfo:bookInfos){
            bookInfo.setStatusCn(BookStatus.getByCode(bookInfo.getStatus()));
        }
        return new PageResult<>(bookInfos,count);
    }
    //只查找一条数据
    public BookInfo selectOne(Integer id) {
        BookInfo bookInfo=bookInfoMapper.querySelectOne(id);
        return bookInfo;
    }

    public Integer update(BookInfo bookInfo) {
        Integer updateData=bookInfoMapper.queryUpdate(bookInfo);
        return updateData;
    }
    //删除数据
    public Integer delete(Integer id) {
        Integer delete=bookInfoMapper.queryDelete(id);
        return delete;
    }
    //批量删除
    public Integer batchDelete(List<Integer> id) {
        Integer batchDelete=bookInfoMapper.queryBatchBook(id);
        return batchDelete;
    }
}
